package com.sxm.yiwei.fragments;


import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.sxm.yiwei.R;
import com.sxm.yiwei.activitis.BillCryBookActivity;
import com.sxm.yiwei.activitis.BillDispathingActivity;
import com.sxm.yiwei.activitis.BillInoverActivity;
import com.sxm.yiwei.activitis.BillOverActivity;
import com.sxm.yiwei.activitis.BillPickupActivity;
import com.sxm.yiwei.activitis.BillSelectBookActivity;

import com.sxm.yiwei.tools.ErrType;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * 我的fa单
 * A simple {@link Fragment} subclass.
 */
public class ShopBillFragment extends Fragment implements View.OnClickListener{

    private RelativeLayout billcrybook;
    private TextView billtextbook;
    private RelativeLayout billselectbook;
    private RelativeLayout billpickuq;
    private RelativeLayout billdispaiching;
    private RelativeLayout billinover;
    private RelativeLayout billover;
    private View billback;
    private SharedPreferences sharedPreferences;
    private String appId;
    private String userId;
    private String token;
    private TextView repickuptext;
    private TextView reselectText;
    private TextView inoverText;
    private TextView overText;
    private TextView dispatchingtext;
    private final static int PEOPLE = 2000;

    public ShopBillFragment() {
        // Required empty public constructor
    }

    @Override
    public void onStart() {
        super.onStart();
        getBillNumber();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_shop_bill, container, false);

        sharedPreferences = getActivity().getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid", "0");
        userId = sharedPreferences.getString("userId", "1");
        token = sharedPreferences.getString("token", "2");

        initBillView(view);
        // 获取接单数量
        getBillNumber();

        return view;

    }
    // 获取接单数量
    private void getBillNumber() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.SENDBOOK_NONBER);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
                String data = obj.getString("data");
                com.alibaba.fastjson.JSONObject obj1 = JSON.parseObject(data);

                if (obj1!=null){
                    String reChoose = obj1.getString("reChoose");
                    String end = obj1.getString("end");
                    String published = obj1.getString("published");
                    String rePickup = obj1.getString("rePickup");
                    String pickup = obj1.getString("pickup");
                    String delivery = obj1.getString("delivery");

                    if (reChoose==null){
                        // 1
                        reselectText.setText("0");
                    }else {
                        reselectText.setText(reChoose);
                    }
                    if (end==null){
                        overText.setText("0");
                    }else {
                        overText.setText(end);
                    }
                    if (published==null){
                        //1
                        billtextbook.setText("0");
                    }else {
                        billtextbook.setText(published);
                    }
                    if (rePickup==null){
                        //2
                        repickuptext.setText("0");
                    }else {
                        repickuptext.setText(rePickup);
                    }
                    if (delivery==null){
                        // 4
                        inoverText.setText("0");
                    }else {
                        inoverText.setText(delivery);
                    }
                    if (pickup==null){
                        dispatchingtext.setText("0");
                    }else {
                        dispatchingtext.setText(pickup);
                    }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(getActivity(),ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initBillView(View view) {
        // 待抢单 显示单数
        billcrybook = (RelativeLayout) view.findViewById(R.id.bill_crybook);
        billselectbook = (RelativeLayout) view.findViewById(R.id.bill_selectbook);
        billpickuq = (RelativeLayout) view.findViewById(R.id.bill_pickup);
        billdispaiching = (RelativeLayout) view.findViewById(R.id.bill_dispatching);
        billinover = (RelativeLayout) view.findViewById(R.id.bill_inover);
        billover = (RelativeLayout) view.findViewById(R.id.bill_over);

        billtextbook = (TextView) view.findViewById(R.id.bill_textbook);
        repickuptext = (TextView) view.findViewById(R.id.bill_repickuptext);
        reselectText = (TextView) view.findViewById(R.id.bill_reselectbook);
        inoverText = (TextView) view.findViewById(R.id.bill_inovertext);
        overText = (TextView) view.findViewById(R.id.bill_overtext);
        dispatchingtext = (TextView) view.findViewById(R.id.bill_dispatchingtext);

        billcrybook.setOnClickListener(this);
        billselectbook.setOnClickListener(this);
        billpickuq.setOnClickListener(this);
        billdispaiching.setOnClickListener(this);
        billinover.setOnClickListener(this);
        billover.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bill_crybook:
                startActivity(new Intent(getActivity(), BillCryBookActivity.class));
                break;
            case R.id.bill_selectbook:
                startActivity(new Intent(getActivity(), BillSelectBookActivity.class));
                break;
            case R.id.bill_pickup:
                startActivity(new Intent(getActivity(), BillPickupActivity.class));
                break;
            case R.id.bill_dispatching:
                startActivity(new Intent(getActivity(), BillDispathingActivity.class));
                break;
            case R.id.bill_inover:
                startActivity(new Intent(getActivity(), BillInoverActivity.class));
                break;
            case R.id.bill_over:
                startActivity(new Intent(getActivity(), BillOverActivity.class));
                break;
        }
    }
}
